var createError = require('http-errors');
var   express      = require('express');
var   path         = require('path');
var   cookieParser = require('cookie-parser');
var   logger       = require('morgan');
var   cors         = require('cors');
const mongoose     = require('mongoose');
mongoose.connect('mongodb://localhost/alen'); //连接数据库

//引入
var indexRouter       = require('./routes/index');
var usersRouter       = require('./routes/users');
var newsRouter        = require('./routes/news');
var productRouter     = require('./routes/product');
var orderRouter       = require('./routes/order');
var newscommentRouter = require('./routes/newscomment');
var procommentRouter  = require('./routes/procomment');
var cateRouter        = require('./routes/cate');
var cartRouter        = require('./routes/cart');

// var loginRouter = require('./routes/admin');
// var loginRouter=require()
var app = express();
// view engine setup
//使用
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(cors()); //函数调用

//解决图片上传大小的问题
var bodyParser = require('body-parser');

 app.use(bodyParser.urlencoded({
    parameterLimit: 100000,
    limit         : '50mb',
    extended      : true
  }));


app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/news', newsRouter);
app.use('/product', productRouter);
app.use('/order', orderRouter);
app.use('/newscomment', newscommentRouter);
app.use('/procomment', procommentRouter);
app.use('/cates', cateRouter);
app.use('/cart', cartRouter);





// app.use('/admin', loginRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    next(createError(404));
});
app.use(function(err, req, res, next) {
    res.locals.message = err.message;
    res.locals.error   = req.app.get('env') === 'development' ? err : {};

    res.status(err.status || 500);
    res.render('error');
});
module.exports = app;